var o={};//模板用到
var GOLBAL={};
function Core(){
	this.loading_logo = $('#ajax_loader');
	this.msg_box = $('#msg_box');
	this.init();
}
Core.prototype = 
{
	init : 
	function(){

	},

	log : 
	function(debugdata) {
		window.console && window.console.log && window.console.log(debugdata);
	},

	ajax :
	function(){
		
		var url = arguments[0];
		var callback = arguments[1];
		var data = arguments[2]?arguments[2]:{};
		var type = (arguments[3]&&arguments[3].toUpperCase()=='POST')?'POST':'GET';
		var options = arguments[4];
		var self = this;
		var opts = {
			url      : url,
			async    : true,
			type     : type,
			data     : data,
			dataType : 'json',
			cache    : false,
			lock     : true,
			force    : false,
			silent   : false
		}
		opts.success = function(data){
			opts.lock && self.loading(false);//返回数据后隐藏进度条
			if(data){
				data.debug && self.log(data.debug);
				if(data.error){
					self.log(data.errorData);
					if(!opts.force){
						return;
					}
				}
				GOLBAL.callbackdata=data;
				callback();
				delete data;
			}
		}
		if(typeof(options) == 'object'){
			opts = $.extend({}, opts, options);
		}
		opts.lock && this.loading(true);//请求时显示进度条
		$.ajax(opts);
	},

	loading : 
	function(show) {
		this.loading_logo.toggle(show);//toggle:切换隐藏状态
	},
	msg :
	function(show) {
		this.msg_box.toggle(show);
	},

	load :
	function(show) {
		var url = arguments[0];
		var data = arguments[1];
		var tpl = arguments[2];
		var act = arguments[3];
		var target = arguments[4];
		var cb = arguments[5];
		var type;
		if(!tpl){
			alert('no template param!');
			return false;
		}
		if(!target){
			target='#main';
		}
		if(url==''){
			tplhtml = eval(tpl+".load(\'"+act+"\');");
			(function(t){
				$(t).html(tplhtml);
			})(target);
			return true;
		}
		if(typeof(data)=='object'){
			type = 'POST';
		}else{
			type = 'GET';
		}
		var ajaxcb = function(){
			var tplhtml;
			if(GOLBAL.callbackdata.status==1){
				o = GOLBAL.callbackdata.data;
				tplhtml = eval(tpl+".load(\'"+act+"\');");
				if(cb){cb();}
			}else if(GOLBAL.callbackdata.status==0){
				o = {};
				tplhtml = eval(tpl+".load(\'"+act+"\');");
				if(cb){cb();}
			}else{
				alert('no data!');
				return false;
			}
			(function(t){
				$(t).html(tplhtml);
			})(target);
		}
		this.ajax(url,ajaxcb,data,type,{});
		return true;
	}
}
